package com.example.lib_common.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import com.example.lib_common.base.state.Empty
import com.example.lib_common.base.state.Failed
import com.example.lib_common.base.state.Loading
import com.example.lib_common.base.state.RequestEvent
import com.example.lib_common.base.state.Success
import com.example.lib_common.observeWithLifecycle
import kotlinx.coroutines.flow.StateFlow

abstract class BaseFragment <VM:BaseViewModel,VDB: ViewDataBinding>: Fragment() {
    protected lateinit var mBinding: VDB //数据绑定
    protected lateinit var mParentView: View //view
    protected abstract val mLayoutResId: Int //布局id
    open val mPageCanBack: Boolean = true
    val mCanBack: Boolean //fragment嵌套
        get() {
            //可返回
            if (mPageCanBack) {
                return true
            }
            //不可返回
            val fragments = childFragmentManager.fragments //当前fragment是否有子fragment
            if (fragments.isEmpty()) {
                return false
            }
            for (fragment in fragments) {
                if (fragment is BaseFragment<*, *>) {
                    if (fragment.isVisible) {
                        return fragment.mPageCanBack
                    }
                }
            }
            return mPageCanBack
        }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mBinding = DataBindingUtil.inflate(layoutInflater, mLayoutResId, container, false)
        mBinding.lifecycleOwner = viewLifecycleOwner
        mParentView = mBinding.root
        return mParentView
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initListener()
        initData()
    }

    open fun initView() {}
    open fun initData() {}
    open fun initListener() {}

    /**
     * 隐藏键盘
     */
    fun hideSoftKeyBroad(view: View) {
        if (requireActivity() is BaseActivity<*, *>) {
            (requireActivity() as BaseActivity<*, *>).hideKeyboard(view)
        }
    }

    /**
     * 显示键盘
     */
    fun dismissKeyboard() {
        if (activity is BaseActivity<*, *>) {
            (requireActivity() as BaseActivity<*, *>).dismissKeyboard()
        }
    }


    /**
     * 显示键盘
     */
    fun showSoftKeyBroad(view: View) {
        if (activity is BaseActivity<*, *>) {
            (requireActivity() as BaseActivity<*, *>).showSoftKeyBoard(view)
        }
    }

    open fun setLoadingViewVisible(show: Boolean, msg: String = "") {
        if (activity is BaseActivity<*, *>) {
            (requireActivity() as BaseActivity<*, *>).setLoadingViewVisible(show, msg)
        }
    }

    /**
     * 通用状态监听：Loading显示和隐藏、错误提示
     */
    open fun addCommonStateListener(state: StateFlow<RequestEvent>) {
        state.observeWithLifecycle(this) {
            when (it) {
                is Loading -> {
                    setLoadingViewVisible(true)
                }

                is Failed -> {
                    setLoadingViewVisible(false)
                    (requireActivity() as BaseActivity<*, *>).showToast(it.message)

                }

                is Success -> {
                    setLoadingViewVisible(false)
                }

                is Empty -> {
                    setLoadingViewVisible(false)
                    (requireActivity() as BaseActivity<*, *>).showToast("数据为空")
                }
            }
        }
    }


}